/*
 * @Author: wangming
 * @Date: 2021-04-13 18:09:08
 * @LastEditors: wangming
 * @LastEditTime: 2022-07-14 16:32:17
 * @Description: 后台获取菜单生成本地routers
 */
import { getHmuserAuth } from '@/apis/paas/HmUserController.js'
import { arrayToTree } from '@/utils/util'
import routes from 'vue-auto-routing'
import i18n from '@/i18n.js'
import store from '@/store/index'
const Index = () => import('./appTemplate/index.vue')
const Template = () => import('./appTemplate/template.vue')
export async function getAuthMenus() {
  let appMenuList = await getHmuserAuth()
  // 过滤APP页面
  appMenuList = appMenuList?.filter((item => item.pageType !== 'APP' && !item.hidden))
  if (appMenuList.length > 0) {
    var appMenus = arrayToTree(appMenuList, null)
    const sysAuth = appMenuList
      .filter((i) => !!i.pathName && i.type == 'SUB')
      .map((i) => i.pathName)
    const setInfoList = getTreeNodeList(routes, sysAuth)
    appMenus = transformTree(appMenus, setInfoList)
    // 针对APP内部流程配置的特殊处理
    if (sysAuth.includes('appSetting-process')) {
      appMenus.push(routes.find(item => item.name === 'processDesign'))
    }
    if (appMenus.length > 0) {
      return { appMenus }
    }
  }
  store.dispatch('goWelcomePage')
  return Promise.reject()

}
/**
 *菜单tree  递归获取路由tree
 * @param {*} tree
 * @param {*} routes
 * @returns
 */
function transformTree(tree, setInfo) {
  return tree
    .filter((i) => !!i?.children?.length || !!i.pageId || !!i.pathName)
    .map((i) => {
      if (i?.children?.length) {
        let children = transformTree(i.children)
        if (i.pathName === 'appSetting' || i.pathName === 'organizationPermission') {
          // 我的应用内置菜单相关的详情页面  组织权限的详情页面
          children = children.concat(setInfo)
        }
        if (changeView(i)) {
          return Object.assign({}, changeView(i), {
            children
          })
        }
      } else {
        return changeView(i)
      }
    })
    .filter((i) => !!i)
}

/**
 * 菜单tree  转化为router 函数
 * @param {*} view
 * @param {*} routes
 * @returns
 */
function changeView(view) {
  if (view.pathName) {
    return getTreeNode(routes, view.pathName, view.icon)
  }
  const { name, icon, pageId, id, target, address, withToken, localism } = view
  return {
    name: `app${id}`,
    path: pageId ? id + '' : `app${id}`,
    meta: { dynamic_page: true, showMenu: true, title: i18n.t(localism) || name, pageId, icon, id, target, address, withToken },
    component: pageId ? Index : Template
  }
}

/**
 * @description  从路由tree获取 需要的route节点
 * @author wangming
 * @date 2021-04-22
 * @param {*} routesTree 路由tree
 * @param {*} pathName 需要获取对tree 节点
 * @returns   返回对象name 对route 节点
 */
function getTreeNode(routesTree, pathName, icon) {
  for (let i = 0; i < routesTree.length; i++) {
    const { name, path, meta = {}, component, children } = routesTree[i]
    if (name === pathName) {
      !!icon && (meta.icon = icon)
      let { title, localism } = meta
      meta.title = i18n.t(localism) || title
      return { name, path, meta, component }
    } else if (pathName.indexOf(name) > -1 && !!children?.length) {
      return getTreeNode(children, pathName, icon)
    }
  }
}
/**
 * @description  因为后台没有存内置菜单页面相关对详情页面手动从路由tree获取相对内置应用列表页详情页面路由集合
 * @author wangming
 * @date 2021-04-22
 * @param {*} routes
 * @param {*} authList
 */
function getTreeNodeList(routes, authList) {
  return routes
    .map((i) => {
      if (i?.children?.length) {
        return getTreeNodeList(i.children, authList)
      } else {
        const isInfo = authList.some(
          (j) => j !== i.name && i.name.indexOf(j + '-') > -1
        )
        if (isInfo) {
          return i
        }
      }
    })
    .flat()
    .filter((i) => !!i)
}
